package com.cai.oamanager.controller;

import com.cai.oamanager.entity.*;
import com.cai.oamanager.service.*;
import com.cai.oamanager.utils.Uploadpath;
import com.cai.oamanager.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/16 17 : 03
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private DepartService departService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private OfficeService officeService;
    private final Integer size=5;

    /**
     *
     * @param model
     * @param tname  按照名字模糊查询
     *
     *  如果名字不为空，则mybatis语句会拼接上模糊查询语句，
     *
     * @param currentPage  当前页号
     *
     * @return
     */
    @RequestMapping("teacher.html")
    public String allPeople(Model model,@RequestParam(defaultValue = "") String tname,@RequestParam(defaultValue = "1") Integer currentPage){
        int start = (currentPage-1)*size;
        List<People> people = userService.allPeople(tname,start,size);
        int teacherCount = userService.getTeacherCount(tname);
        int totalPage=1;
        if(teacherCount!=0) {

            totalPage = teacherCount % size == 0 ? teacherCount / size : teacherCount / size + 1;
            System.out.println(totalPage+" :: "+teacherCount);
        }

//        if(!tname.equals("")){
//            int teacherCountByLike = userService.getTeacherCountByLike(tname);
//            totalPage = 1;
//        }
        model.addAttribute("peopleList",people);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("tname",tname);
        return "teacher";
    }

    /**
     * 跳转到添加页面
     * @param model
     * @return
     */
    @RequestMapping("toAddPeople")
    public String toAddPeople(Model model){
        List<Position> allPosition = positionService.getAllPosition();
        Integer countDepart = departService.getCountDepart();
        List<Depart> allDepart = departService.getAllDepart(0,countDepart);
        List<Course> allCourse = courseService.getAllCourse();
        List<Office> allOffice = officeService.getAllOffice();
        model.addAttribute("positionList",allPosition);
        model.addAttribute("departList",allDepart);
        model.addAttribute("courseList",allCourse);
        model.addAttribute("officeList",allOffice);
        return "addTeacher";
    }

    /**
     * 添加
     * @param people
     * @return
     */
    @RequestMapping("addPeople")
    public String addPeople(People people){
        people.setOid(Utils.getUUID());
        people.setOintime(new Date());
        people.setImg("huhou-002.jpg");
        people.setOpassword("123456");
        if(people.getOposition()==3){
            people.setOcourse(0);
        }
        people.setOauth(2);
        userService.addPeople(people);
        return "redirect:/user/teacher.html";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id){
        System.out.println("id:  "+id);
        userService.delete(id);
        return "redirect:/user/teacher.html";
    }

    /**
     * 修改自己的信息
     */
    @RequestMapping("updateSelf")
    public String updateSelf(HttpServletRequest request, HttpServletResponse response, MultipartFile file, People people){
        if (!file.isEmpty()){
            String filename = file.getOriginalFilename();
            System.out.println("文件名："+filename);
            String extension = filename.substring(filename.lastIndexOf("."));
            filename = Utils.getUUID()+extension;
            String uploadPath = Uploadpath.getUploadPath();
            people.setImg(filename);
            try {
                System.out.println("路径："+uploadPath);
                file.transferTo(new File(uploadPath,filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            People people1 = (People) request.getSession().getAttribute("people");
            people.setImg(people1.getImg());
        }
        System.out.println(people.toString());
        userService.updateSelf(people);
        request.getSession().removeAttribute("people");
//        response.setHeader("refresh","0;login.html");
        return "redirect:/login/outLogin";
    }

    @RequestMapping("toEdit/{oid}")
    public String editTeacher(Model model){

        return "editTeacher";
    }

}
